<?php
/**
*+------------------APP 用户中心类----------------+
*--------------------------------------------------+
*+---------用户他人个人中心数据展示操作------------+
*+-------------------------------------------------+
*+-@author susy.liang <liangling@ilongyuan.com.cn>-+
*+-------------------------------------------------+
*/
namespace Api\Controller;

class OthersController extends PublicController {

	/**
	*他人的标识
	*/
	private $otherId = '';
	// private $nickname = '';

	/**
	*操作用户的标识
	*/
	private $selfId = '';

	/**
	*基础参数的获取
	*@author susy.liang <liangling@ilongyuan.com.cn>
	*/
	protected function _initialize(){
		parent::_initialize();

		$nickname = I('nickname', '');
		if(!$nickname)
			$this->error(self::$API_ERROR['PARAM'], '参数有误');
		$this->nickname = $nickname;
		$user = A('Api/User', 'Event')->getUser($nickname);
		if(!$user)
			$this->error(self::$API_ERROR['USER'], '用户有误');

		$this->otherId = intval($user);

		$appToken = I('appToken', '', 'strip_tags');
		$uid = $this->getUid($appToken);
		if($uid)
			$this->selfId = intval($uid);

	}

	/**
	*用户的个人资料
	*@author susy.liang <liangling@ilongyuan.com.cn>
	*/
	public function index(){

		$data = A('Api/User', 'Event')->detail($this->otherId, true);
		$data['is_follow'] = 0;
		if($this->selfId)
		{
			$data['is_follow'] = intval(A('Api/Follow', 'Event')->is_follow($this->selfId, $this->otherId));
			$data['is_zan']    = (int)event('Like')->is_like_user($this->selfId, $this->otherId);
		}

		$this->success('操作成功', $data);
	}

	/**
	*用户的动态
	*@author susy.liang <liangling@ilongyuan.com.cn>
	*/
	public function trade(){

		$servVideo = D('Common/Video', 'Service');

		//$timestamp = I('timestamp', 0, 'intval');
		$p = I('p', 1, 'intval');
		$pager = I('pager', C('HOME_VIDEO_PER',NULL, 20), 'intval');

		$videos = array();
		$uflag = $this->isEditer();
		// if($uflag === true){  // 全部取用发布视频
			//取发布的
		$map = array('uid'=>$this->otherId);
		$map['race_id'] = array('egt', null);
		$total = $servVideo->where($map)->count();
		if($total){
			$videos = $servVideo->where($map)->page($p)->order('create_time desc')->limit($pager)->getField('id', true);
		}
			
		// }else{
		// 	$servFv = D('Common/Favour', 'Service');
		// 	$map = array('uid'=>$this->otherId);
		// 	$total = $servFv->where($map)->count();
		// 	if($total){
		// 		$videos = $servFv->where($map)->page($p)->limit($pager)
		// 				->getField('video_id', true);						
		// 	}
		// }

		$lists = $this->lists_format($videos);

		$data = array();
		$data['count'] = $total;
	 	foreach ($lists as $key => $value) {
	 		$data['lists'][] = array(
	 			'title' => $value['title'],
	 			'thumb' => $value['thumb'],
	 			'type' => $value['type'] ? $value['type'] : 0,
	 			'data' => $value,
	 		);
	 	}
	 	$this->success('操作成功', $data);

	 	#TODO 使用老版本  新开接口做
		##  他人动态加入配音 签名 
		#{ 'type', 'data', 'create_time', 'memo' }
		
		// $p 		= I('p', 1, 'intval');
		// $pager  = I('pager', C('HOME_VIDEO_PER',NULL, 20), 'intval');
		// $res   = service('UserOper')->where(['uid' => $this->otherId])->order('id desc')->limit($pager)->page($p)->select();
		// $list = [];
		// foreach ($res as $key => $value) {
		// 	$data = [];
		// 	$data['type'] 		 = $value['type'];
		// 	$data['create_time'] = $value['create_time'];
		// 	$data['memo'] 		 = $value['memo'];
		// 	$data['data'] = '';
		// 	switch ($value['type']) {
		// 		case 'voice':
		// 			$data['data'] = $value['extends'];
		// 			break;
		// 		case 'dubbing':
		// 			$data['data'] = $value['extends'];
		// 			break;
		// 		default:
		// 			// video
		// 			$video_ids  	= [$value['extends']];
		// 			$videos 		= $this->lists_format($video_ids);
		// 			$data['data'] 	= current($videos);
		// 			break;
		// 	}
		// 	$list[] = $data;
		// }
		// $this->list_success($list);
	}

	/**
	*判断是否小编用户
	*@author susy.liang <liangling@ilongyuan.com.cn>
	*/
	private function isEditer(){

		$homeGrp = M('auth_group')->where(array('module'=>'home'))->getField('id');
		if($homeGrp){
			
			$map['group_id'] = $homeGrp;
			$map['uid']      = $this->otherId;
			return M('auth_group_access')->where($map)->find() ? true : false;

			// $user = M('auth_group_access')->where(array('group_id'=>array('in', $homeGrp)))->getField('uid', true);
			// if(in_array($this->otherId, $user))
				// return true;
		}

		return false;
	}

	/**
	*用户的粉丝
	*@author susy.liang <liangling@ilongyuan.com.cn>
	*/
	public function fans(){

		$fevtObj = A('Api/Follow', 'Event');
		$total = $fevtObj->count_follows($this->otherId);
		if($total){
			$uEvent = A('Api/User','Event');
			$p = I('p', 1, 'intval');
			$pager 	   = I('pager', C('USER_LIST_NUM',NULL, 10), 'intval');
			$fans = $fevtObj->follows($this->otherId, false, array(), $pager, $p);
			foreach ($fans as $key => $value) {
				$user = $uEvent->detail($value);
				if(!$user) continue;
				$user['is_follow'] = intval($fevtObj->is_follow($this->selfId, $value));
				$followers[] = $user;
			}

			$data = array('list'=>$followers, 'total'=>$total);
		}
		$this->success('操作成功', $data);

	}

	/**
	*用户的好友就，即用户的关注
	*@author susy.liang <liangling@ilongyuan.com.cn>
	*/
	public function friend(){
		$fevtObj = A('Api/Follow', 'Event');

		$total = $fevtObj->count_mine($this->otherId);
		if($total){
			$uEvent = A('Api/User','Event');

			$p = I('p', 1, 'intval');
			$pager 	   = I('pager', C('USER_LIST_NUM',NULL, 10), 'intval');
			$mine = $fevtObj->mine($this->otherId, false, array(), $pager, $p);
			foreach ($mine as $key => $value) {
				$user = $uEvent->detail($value);
				$user['is_follow'] = intval($fevtObj->is_follow($this->selfId, $value));
				$followers[] = $user;
			}

			$data = array('list'=>$followers, 'total'=>$total);
		}

		$this->success('操作成功', $data);
	}

	/**
	*用户订阅的游戏
	*@author susy.liang <liangling@ilongyuan.com.cn>
	*/
	public function game(){

		$gsevtObj = A('Api/GameScribe', 'Event');
		$games = $gsevtObj->mine($this->otherId, true);

		if($games){
			$data = array();
			foreach ($games as $key => $value) {

				uri_format($value['pic']);
				uri_format($value['cover']);

				if($this->selfId)
					$value['is_scribe'] = intval($gsevtObj->is_scribe($this->selfId, $value['id']));
				else
					$value['is_scribe'] = 0;

				array_push($data, $value);
			}
			
		}

		$this->success('操作成功', $data);
	}


	public function news()
	{
		##  他人动态加入配音 签名 
		#{ 'type', 'data', 'create_time', 'memo' }
		
		$p 		= I('p', 1, 'intval');
		$pager  = I('pager', C('HOME_VIDEO_PER',NULL, 20), 'intval');
		$res   = service('UserOper')->where(['uid' => $this->otherId])->order('id desc')->limit($pager)->page($p)->select();
		$list = [];
		foreach ($res as $key => $value) {
			$data = [];
			$data['type'] 		 = $value['type'];
			$data['create_time'] = $value['create_time'];
			$data['memo'] 		 = $value['memo'];
			$data['data'] = '';
			switch ($value['type']) {
				case 'voice':
					$data['data'] = $value['extends'];
					break;
				case 'dubbing':
					$data['data'] = $value['extends'];
					break;
				default:
					// video
					$video_ids  	= [$value['extends']];
					$videos 		= $this->lists_format($video_ids);
					$data['data'] 	= current($videos);
					break;
			}
			$list[] = $data;
		}
		$this->list_success($list);
	}



}